package com.example.administrator.myapplication;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.example.administrator.myapplication.interf.BackHandleInterface;
import com.example.administrator.myapplication.service.InitSettings;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GloabFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GloabFragment extends MyBaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String TAG = "GloabFragment";
    private ActionBar actionBar;
    public static final String PREFS_NAME = InitSettings.SETTINGS_GLOAB;
    private Switch transverse_screen_switch;
    private boolean transverse_screen_state;  //屏幕方向
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private BackHandleInterface mBackHandleInterface;

    public GloabFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GloabFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GloabFragment newInstance(String param1, String param2) {
        GloabFragment fragment = new GloabFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if(!(getActivity() instanceof BackHandleInterface)){
            throw new ClassCastException(" 父Activity没有实现 BackHandleInterface 接口 ");
        }else {
            this.mBackHandleInterface = ((BackHandleInterface) getActivity());
        }
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        getActivity().setTitle(getString(R.string.title_gloab));
        actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setHasOptionsMenu(true);
        transverse_screen_state = InitSettings.getInstance(getActivity()).getTransverse_screen_state();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gloab, container, false);
        transverse_screen_switch = ((Switch) view.findViewById(R.id.Transverse_screen_switch));
        //默认开关处于关闭状态
        transverse_screen_switch.setChecked(transverse_screen_state);

        transverse_screen_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //将值临时存储于变量中以方便后续持久化
                    transverse_screen_state = true;
                    Toast.makeText(getActivity(),"开启",Toast.LENGTH_SHORT).show();
                    //通过代码实时设置横向屏幕
                    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }else {
                    transverse_screen_state = false;
                    Toast.makeText(getActivity(),"关闭",Toast.LENGTH_SHORT).show();
                    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

                }
            }
        });
        // Inflate the layout for this fragment
        return view;
    }

    @Override
    public void onStart() {
        mBackHandleInterface.setSelectedFragment(this);
        super.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //将设置的数据持久化至SharePreferences中
        /*SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(InitSettings.Transverse_screen_state_NAME, transverse_screen_state);
        editor.commit();*/
        InitSettings.getInstance(getActivity()).setTransverse_screen_state(transverse_screen_state);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                Toast.makeText(getActivity(),"回退至 设置 界面",Toast.LENGTH_SHORT).show();
                Log.i(TAG, "将当前视图从 回退栈 中弹出！");
                 getActivity().onBackPressed();
                return true;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    public void back(){

    }

}
